package com.skyc.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.rowset.CachedRowSet;

import com.skyc.util.JDBCUtil;
import com.skyc.dao.AddressDao;
import com.skyc.dao.UserDao;
import com.skyc.entity.User;

public class UserDaoImpl implements UserDao {
	JDBCUtil util = JDBCUtil.getJDBCUtil();
	@Override
	public User loginByname(String name, String pwd) {
		User user = null;
		CachedRowSet crs = util.query("select * from t_user where u_name=? and  u_pwd=?", name, pwd);
		return returnUser(crs);
	}
//手机号登录
	@Override
	public User loginByphone(String phone, String pwd) {
		User user=null;
		CachedRowSet crs = util.query("select * from t_user where u_phone=? and  u_pwd=?", phone, pwd);	
		return returnUser(crs);
	}
	
	//工具
	@Override
	public User queryById(String id) {
		String sql = "select * from t_user where u_id=?";
		CachedRowSet crs = util.query(sql, id);
		return returnUser(crs);
	}
	
	//工具
	private User returnUserUtil(CachedRowSet crs) throws SQLException {
		User user = new User();
		user.setUid(crs.getInt("u_id"));
		user.setUname(crs.getString("u_name"));
		user.setUpwd(crs.getString("u_pwd"));
		user.setUrealname(crs.getString("u_realname"));
		user.setUemail(crs.getString("u_email"));
		user.setUphone(crs.getString("u_phone"));
		user.setUidcard(crs.getString("u_idcard"));
		user.setUimgurl(crs.getString("u_imgurl"));
		return user;
	}
	
	//工具
	private User returnUser(CachedRowSet crs) {
		User user = null;
		try {
			if (crs.next()) {
				user = returnUserUtil(crs);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			util.closeCacheRowSet(crs);
		}
		return user;
	}
	
	//工具
	private List<User> returnUsers(CachedRowSet crs) {
		List<User> users = new ArrayList<User>();
		try {
			while (crs.next()) {
				users.add(returnUserUtil(crs));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			util.closeCacheRowSet(crs);
		}
		return users;
	}

	
	
	//注册
	@Override
	public User registerUser(User user) {
		 boolean flag=util.update("insert into t_user (u_name,u_pwd,u_phone)values(?,?,?)", user.getUname(),user.getUpwd(),user.getUphone());
		    if(flag) {
		    	return loginByphone(user.getUphone(),user.getUpwd());
		    }
			return null;
		
	}
	//找回密码
	@Override
	public User resetpwdByphone(User user) {
		
		 boolean flag=util.update("update  t_user set u_pwd=? where u_phone=?", user.getUpwd(),user.getUphone());
		    if(flag) {
		    	return loginByphone(user.getUphone(),user.getUpwd());
		    }
			return null;
	}
	//根据手机号查询用户
	@Override
	public boolean queryByPhone(String phone) {
		CachedRowSet crs=util.query("select * from t_user where u_phone=?", phone);
		try {
			if(crs.next()) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			util.closeCacheRowSet(crs);
		}
		return false;
	}
	//根据手机号查询用户并返回用户信息
	@Override
	public User queryByphoneUser(String phone) {
		CachedRowSet crs=util.query("select * from t_user where u_phone=?", phone);
		return returnUser(crs);
	}
	

}
